package org.ithink.util;

/**
 * HTML相关的工具类
 * 
 * 不扩展、不改变、不继承
 */
public final class HtmlUtil {

  /**
   * 私有，不允许实例化
   */
  private HtmlUtil() {
  }

  /**
   * 转义字符
   */
  public static String filter(String message) {

    if (message == null) {
      return (null);
    }

    char content[] = new char[message.length()];
    message.getChars(0, message.length(), content, 0);
    StringBuffer result = new StringBuffer(content.length + 50);
    for (int i = 0; i < content.length; i++) {
      switch (content[i]) {
        case '<' :
          result.append("&lt;");
          break;
        case '>' :
          result.append("&gt;");
          break;
        case '&' :
          result.append("&amp;");
          break;
        case '"' :
          result.append("&quot;");
          break;
        default :
          result.append(content[i]);
      }
    }
    return (result.toString());

  }

  /**
   * HTML中的元素常量
   * 
   * 也可以直接用String，用类的原因是为了区分
   */
  public static class Tag {

    public static final Tag A = new Tag("a");
    public static final Tag ABBR = new Tag("abbr");
    public static final Tag ACRONYM = new Tag("acronym");
    public static final Tag ADDRESS = new Tag("address");
    public static final Tag APPLET = new Tag("applet");
    public static final Tag BASE = new Tag("base");
    public static final Tag BLOCKQUOTE = new Tag("blockquote");
    public static final Tag BODY = new Tag("body");
    public static final Tag BR = new Tag("br");
    public static final Tag BUTTON = new Tag("button");
    public static final Tag CAPTION = new Tag("caption");
    public static final Tag CITE = new Tag("cite");
    public static final Tag CODE = new Tag("code");
    public static final Tag COMMENT = new Tag("comment");
    public static final Tag DD = new Tag("dd");
    public static final Tag DEL = new Tag("del");
    public static final Tag DFN = new Tag("dfn");
    public static final Tag DIR = new Tag("dir");
    public static final Tag DIV = new Tag("div");
    public static final Tag DL = new Tag("dl");
    public static final Tag DT = new Tag("dt");
    public static final Tag EM = new Tag("em");
    public static final Tag HEAD = new Tag("head");
    public static final Tag FORM = new Tag("form");
    public static final Tag FIELDSET = new Tag("fieldset");
    public static final Tag HTML = new Tag("html");
    public static final Tag H1 = new Tag("h1");
    public static final Tag H2 = new Tag("h2");
    public static final Tag H3 = new Tag("h3");
    public static final Tag H4 = new Tag("h4");
    public static final Tag H5 = new Tag("h5");
    public static final Tag H6 = new Tag("h6");
    public static final Tag IFRAME = new Tag("iframe");
    public static final Tag IMG = new Tag("img");
    public static final Tag INPUT = new Tag("input");
    public static final Tag LABEL = new Tag("label");
    public static final Tag LEGEND = new Tag("legend");
    public static final Tag LI = new Tag("li");
    public static final Tag LINK = new Tag("link");
    public static final Tag META = new Tag("meta");
    public static final Tag NOSCRIPT = new Tag("noscript");
    public static final Tag OBJECT = new Tag("object");
    public static final Tag OL = new Tag("ol");
    public static final Tag OPTGROUP = new Tag("optgroup");
    public static final Tag OPTION = new Tag("option");
    public static final Tag PARAM = new Tag("param");
    public static final Tag PRE = new Tag("pre");
    public static final Tag Q = new Tag("q");
    public static final Tag SAMP = new Tag("samp");
    public static final Tag SPAN = new Tag("span");
    public static final Tag SCRIPT = new Tag("script");
    public static final Tag STRONG = new Tag("strong");
    public static final Tag SUMMARY = new Tag("summary");
    public static final Tag TABLE = new Tag("table");
    public static final Tag TITLE = new Tag("title");
    public static final Tag TEXTAREA = new Tag("textarea");
    public static final Tag TBODY = new Tag("tbody");
    public static final Tag TD = new Tag("td");
    public static final Tag TFOOT = new Tag("tfoot");
    public static final Tag THEAD = new Tag("thead");
    public static final Tag TH = new Tag("th");
    public static final Tag TR = new Tag("tr");
    public static final Tag UL = new Tag("ul");
    public static final Tag VAR = new Tag("var");

    /** 元素名称 */
    private final String name;

    /**
     * 私有构造函数，仅用于初始化本身静态常量
     */
    private Tag(final String name) {
      this.name = name;
    }

    /**
     * 返回元素名称
     */
    public String toString() {
      return name;
    }

  }

  /**
   * HTML中的元素的属性常量
   * 
   * 也可以直接用String，用类的原因是为了区分
   */
  public static class Attr {

    public static final Attr ALT = new Attr("alt");
    public static final Attr BORDER = new Attr("border");
    public static final Attr CHECKED = new Attr("checked");
    public static final Attr CLASS = new Attr("class");
    public static final Attr DIR = new Attr("dir");
    public static final Attr DISABLED = new Attr("disabled");
    public static final Attr HREF = new Attr("href");
    public static final Attr ID = new Attr("id");
    public static final Attr LANG = new Attr("lang");
    public static final Attr LONGDESC = new Attr("longdesc");
    public static final Attr NAME = new Attr("name");
    public static final Attr ONBLUR = new Attr("onblur");
    public static final Attr ONCHANGE = new Attr("onchange");
    public static final Attr ONCLICK = new Attr("onclick");
    public static final Attr ONDBLCLICK = new Attr("ondblclick");
    public static final Attr ONFOCUS = new Attr("onfocus");
    public static final Attr ONKEYDOWN = new Attr("onkeydown");
    public static final Attr ONKEYPRESS = new Attr("onkeypress");
    public static final Attr ONKEYUP = new Attr("onkeyup");
    public static final Attr ONLOAD = new Attr("onload");
    public static final Attr ONMOUSEDOWN = new Attr("onmousedown");
    public static final Attr ONMOUSEMOVE = new Attr("onmousemove");
    public static final Attr ONMOUSEOVER = new Attr("onmouseover");
    public static final Attr ONSELECT = new Attr("onselect");
    public static final Attr REL = new Attr("rel");
    public static final Attr SELECTED = new Attr("selected");
    public static final Attr SRC = new Attr("src");
    public static final Attr STYLE = new Attr("style");
    public static final Attr TARGET = new Attr("target");
    public static final Attr TITLE = new Attr("title");
    public static final Attr TYPE = new Attr("type");
    public static final Attr VALUE = new Attr("value");

    /** 属性名称 */
    private final String name;

    /**
     * 私有的，仅用于初始化自身常量
     */
    private Attr(final String name) {
      this.name = name;
    }

    /**
     * 返回属性名称
     */
    public String toString() {
      return name;
    }
  }

}
